package com.jdk.demo.io.半读;

import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	public static void main(String[] args) throws Exception {
		ServerSocket serverSocket = new ServerSocket(8088);
		Socket socket = serverSocket.accept();
		InputStream inputStream = socket.getInputStream();
		System.out.println("A=" + inputStream.available());
		byte[] byteArray = new byte[2];
		int readLength = inputStream.read(byteArray);
		System.out.println("Server取得的数据：" + new String(byteArray, 0, readLength));
		// 作用是将套接字的输入流置于"流的末尾eof"，也就是在套接字上调用shutdownInput方法后从套接字输入流读取内容，流将返回eof。发送到套接字的输入流端的任何数据将在确认后被静默丢弃，
		// 调用此方法的一端进入半读状态，也就是此端不能获得输入流，但对端却能获得输入流。一端能读，另一端不能读，称为半读
		socket.shutdownInput();
		System.out.println("B=" + inputStream.available()); // 静默丢弃其他数据
		readLength = inputStream.read(byteArray); // -1
		System.out.println(readLength);
		// 再次调用getInputStream()方法出现异常: java.net.SocketException: Socket input is shutdown
		socket.getInputStream();

		socket.close();
		serverSocket.close();
	}

}
